home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2161 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.3 KB  |  102 lines

  1. Path: erich.triumf.ca!bennett
  2. From: bennett@erich.triumf.ca (P.Bennett)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Problem with c code, please help!
  5. Date: 19 Jan 1996 08:19 PST
  6. Organization: TRIUMF: Tri-University Meson Facility
  7. Distribution: world
  8. Message-ID: <19JAN199608194071@erich.triumf.ca>
  9. References: <surgsw-1901960148530001@128.206.206.86> <mikedorman-1901960725380001@205.148.200.150>
  10. NNTP-Posting-Host: erich.triumf.ca
  11. News-Software: VAX/VMS VNEWS 1.50    
  12.  
  13. In article <mikedorman-1901960725380001@205.148.200.150>, mikedorman@cedarnet.com (Mike Dorman) writes...
  14. >In article <surgsw-1901960148530001@128.206.206.86>,
  15. >surgsw@mizzou1.missouri.edu (Joel Weinstein) wrote:
  16. >> #include <stdio.h>
  17. >> main()
  18. >> {
  19. >>    int i=0;
  20. >>    char  word[100], c;
  21. >word is an array of 100 chars, there are no pointers declared here.
  22. >change word to this:
  23. >      char *word;
  24. >and it will be *much* easier.
  25.  
  26. But it won't work.  You now have reserved space for a pointer-to-char, but have
  27. not reserved any space for it to point to, not have you set it to point
  28. anywhere meaningful.
  29.  
  30. >>    printf("Enter a word:   ");
  31. >>    while( (c = getchar()) != '\n')  {
  32. >>       *word = c;
  33. >here, replace *word (which is a pointer to something...you didn't specify
  34. >which char in word you wanted it to point to, so it could be pointing to
  35. >just about anything) with
  36. >        strcat(word, *c);
  37.  
  38. strcat() requires two strings - word is still an uninitialized pointer, so
  39. strcat() will write to some unkown location.  This probably won't compile,
  40. since "*c" is invalid - you probably mean "&c", but that's no good either,
  41. since "c" is a single char, not a 0 terminated string.
  42.  
  43.  
  44. >Also, when getting string information from the user, you probably
  45. >shouldn't use a char array (I know it seems to make more sense that way,
  46. >but soon it'll become clear that it doesn't), but you should use a string
  47. >pointer, like this:
  48. >   char *word;
  49. >Now, word is a pointer to a string, which has no specific or limited
  50. >length.  Then you can use the standard functions like strcpy and strcat to
  51. >work on string pointers.
  52.  
  53. No - word is an uninitialized pointer, and no space is reserved for it to point
  54. to - using it  as suggested can only lead to disaster, as you write over random
  55. locations in memory.
  56.  
  57. >To do it with an array, you need another variable to keep track of how
  58. >many characters you've already put into word, so you can index the array
  59. >correctly:
  60.  
  61. <some (surprisingly!) valid code snipped>
  62. >   puts("You entered: ");
  63. >   puts(*word[0]);
  64.  
  65. Most people would use:
  66.     puts(word);
  67. In this case the name of an array acts like a pointer to the first element of
  68. the array.
  69.  
  70. >}
  71. >But, if you did it with a string pointer, it's this much easier:
  72. >main()
  73. >{ 
  74. >   char *word;
  75.  
  76. Where does "word" point??
  77.  
  78. >   puts("Enter a word: ");
  79. >   while(scanf("%s", word) != 0)
  80. This will store the input in some random location (perhaps over-writing some of
  81. the program or operating system  :-(  )
  82.  
  83.  
  84.  
  85. Peter Bennett VE7CEI                | Vessels shall be deemed to be in sight
  86. Internet: bennett@triumf.ca         | of one another only when one can be
  87. Packet: ve7cei@ve7kit.#vanc.bc.ca   | observed visually from the other
  88. TRIUMF, Vancouver, B.C., Canada     |                          ColRegs 3(k)
  89. GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
  90.  
  91.  
  92.  
  93.  
  94.  
  95.